<template>
  <el-dialog
    title="上传主图"
    :visible.sync="dialogVisible"
    :before-close="handleClose"
    :destroy-on-close="true"
    :close-on-click-modal="false"
    width="650px">
    <el-form :model="form" ref="form">
      <el-form-item>
        <image-input v-model="form.images" :max-count="1"></image-input>
        <p style="margin: 0;">建议尺寸大小为： xx*xx</p>
      </el-form-item>
    </el-form>
    <div slot="footer" style="display: flex;justify-content: center;">
      <el-button type="primary"
                 @click="cancel">取消
      </el-button>
      <el-button type="primary"
                 @click="submit">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import imageInput from '@/components/image-input'
import { update } from '../api'

export default {
  name: 'poster',
  data () {
    return {
      dialogVisible: false,
      id: undefined,
      form: {
        images: []
      },
      rule: {
        image: [
          { required: true, message: '请输入标题' }
        ]
      }
    }
  },
  components: {
    imageInput
  },
  methods: {
    handleClose (done) {
      done()
    },
    open (row) {
      const { id, images } = row
      this.id = id
      if (images) {
        Object.assign(this.form, { images })
      }
      this.dialogVisible = true
      return new Promise((resolve) => {
        this._resolve = resolve
      })
    },
    async submit () {
      await this.$refs.form.validate()
      const images = this.form.images.map(({ url }) => ({ url }))
      await update({ id: this.id }, { images })
      this.$message.success('更新成功')
      this.cancel()
    },
    cancel () {
      this.close()
    },
    close () {
      this.dialogVisible = false
      this._resolve({ refresh: true })
    }
  }
}
</script>

<style scoped lang="scss">
</style>
